/*
https://leetcode.cn/problems/maximum-erasure-value/

1695.删除子数组的最长部分
medium, 李佳韵 2024.09.01
滑动窗口

*/


#pragma once
#include <iostream>
#include <string>
#include<vector>
#include <unordered_map>
using namespace std;

class Solution {
public:
    int maximumUniqueSubarray(vector<int>& nums) {
        const int N = nums.size();
        vector<int> pre(N + 1);
        for (int i = 0; i < N; i++)
            pre[i + 1] = nums[i] + pre[i];

        int left = 0, right = 0, res = 0;
        unordered_map<int, int> mp;
        while (right < N) {
            if (mp.find(nums[right]) != mp.end() && mp[nums[right]] >= left) {
                left = mp[nums[right]] + 1;
            }
            res = max(res, pre[right + 1] - pre[left]);
            mp[nums[right]] = right;
            right++;
        }
        return res;
    }
};

int main()
{
    Solution solution;
    vector<int>nums = { 4,2,4,5,6 };
    int result = solution.maximumUniqueSubarray(nums);
    cout << "删除子数组的最大得分为：" << result << endl;
    return 0;
}
